// pages/addresses/addresses.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addresses:[],
    hidden: false,
    scrollTop: 0,
    scrollHeight: 0,
    hasMore: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          scrollHeight: res.windowHeight
        });
      }
    });
    this.getData();
  },

  getData: function () {
    this.addresses();
  },
  addresses: function() {
    this.setData({
      addresses: []
    });
    var that = this;
    wx.request({
      url: getApp().globalData.host + 'address/all.html',
      header: getApp().globalData.header,
      method: 'POST',
      success: function (res) {
        if (res.data == '' || res.data == null || res.data == undefined || res.data.length == 0) {
          that.setData({
            hidden:true
          });
          return;
        }
        that.setData({
          addresses: res.data,
          hidden:true
        });
      },
      fail: function (res) { },
      complete: function (res) { }
    })
  },
  //设置为默认地址
  defaultAddress: function(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    if(this.data.addresses[index].isDefault) return;
    wx.request({
      url: getApp().globalData.host + 'address/setDefault.html',
      data: { id: that.data.addresses[index].addressId },
      method: 'POST',
      header: getApp().globalData.header,
      success: function (res) {
        if (res.data.errorCode == 0) {
          wx.showToast({
            title: "设置成功",
            duration: 2000
          })
          that.addresses();
        } else {
          wx.showToast({
            title: "设置失败",
            duration: 2000
          })
        }
      },
      fail: function (res) { },
      complete: function (res) { }
    })
  },
  //修改收货地址
  modifyAddress: function(e) {
    var addressId = e.currentTarget.dataset.addressid;
    wx.navigateTo({
      url: '../address/address?addressId=' + addressId,
    })
  },
  //删除收货地址
  delAddress: function(e) {
    var that = this;
    var addressId = e.currentTarget.dataset.addressid;
    wx.showModal({
      title: '提示',
      content: '确认要删除该地址吗？',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: getApp().globalData.host + 'address/setDisabled.html',
            data: { id: addressId },
            method: 'POST',
            header: getApp().globalData.header,
            success: function (res) {
              if (res.data.errorCode == 0) {
                wx.showToast({
                  title: "删除成功",
                })
                that.addresses();
              } else {
                wx.showToast({
                  title: "删除失败",
                })
              }
            },
            fail: function (res) { },
            complete: function (res) { }
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})